package com.langyiquan.front.controller;

import com.langyiquan.bo.AddressBO;
import com.langyiquan.exception.http.ParamException;
import com.langyiquan.pojo.UserAddress;
import com.langyiquan.service.AddressService;
import com.langyiquan.utils.UnifyResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Api(value = "地址相关", tags = {"地址相关的相关接口"})
@RestController
@RequestMapping("address")
public class AddressController extends BaseController {

    @Autowired
    private AddressService addressService;

    @ApiOperation(value = "获取用户地址",notes = "获取用户地址",httpMethod = "POST")
    @PostMapping("/list")
    public UnifyResponse comments(HttpServletRequest request){

        String userId = getPassportUserId(request);

        List<UserAddress> list = addressService.getAddressList(userId);
        return UnifyResponse.success(list);

    }

    @ApiOperation(value = "新增用户地址",notes = "新增用户地址",httpMethod = "POST")
    @PostMapping("/add")
    public UnifyResponse add(@RequestBody @Valid  AddressBO addressBO, BindingResult bindingResult){

        if(bindingResult.hasErrors()){
            Map<String,String> errorsMap = getBindingResultErrors(bindingResult);
            String errCode = errorsMap.entrySet().iterator().next().getValue();
            throw new ParamException(Integer.parseInt(errCode));
        }

        addressService.addAddress(addressBO);
        return UnifyResponse.success();

    }


    @ApiOperation(value = "修改用户地址",notes = "修改用户地址",httpMethod = "POST")
    @PostMapping("/update")
    public UnifyResponse update(@RequestBody @Valid AddressBO addressBO, BindingResult bindingResult){

        if(StringUtils.isBlank(addressBO.getAddressId())){
            throw new ParamException(3002);
        }

        if(bindingResult.hasErrors()){
            Map<String,String> errorsMap = getBindingResultErrors(bindingResult);
            String errCode = errorsMap.entrySet().iterator().next().getValue();
            throw new ParamException(Integer.parseInt(errCode));
        }

        addressService.updateAddress(addressBO);
        return UnifyResponse.success();

    }


    @ApiOperation(value = "删除用户地址",notes = "删除用户地址",httpMethod = "POST")
    @PostMapping("/delete")
    public UnifyResponse delete(String addressId, HttpServletRequest request){

        if(StringUtils.isBlank(addressId)){
            throw new ParamException(3002);
        }

        String userId = getPassportUserId(request);

        addressService.deleteAddress(userId,addressId);
        return UnifyResponse.success();

    }


    @ApiOperation(value = "设置默认用户地址",notes = "设置默认用户地址",httpMethod = "POST")
    @PostMapping("/setDefalut")
    public UnifyResponse setDefalut(String addressId, HttpServletRequest request){

        if(StringUtils.isBlank(addressId)){
            throw new ParamException(3002);
        }

        String userId = getPassportUserId(request);

        addressService.setDefalutAddress(userId,addressId);
        return UnifyResponse.success();

    }

}
